#include "ThreeDAxis.h"

ThreeDAxis::ThreeDAxis()
{

}
ThreeDAxis::ThreeDAxis(LPDIRECT3DDEVICE9 device)
{
	m_pDirect3DDevice = device ;
	m_vOrign = D3DXVECTOR3(0.0f, 0.0f, 0.0f) ;
	m_fXAxisLength = 0.5f ;
	m_fYAxisLength = 0.5f ;
	m_fZAxisLength = 0.5f ;
}

ThreeDAxis::ThreeDAxis(LPDIRECT3DDEVICE9 device, D3DXVECTOR3 orign, float xLen, float yLen, float zLen)
{
	m_pDirect3DDevice = device ;
	m_vOrign = orign ;
	m_fXAxisLength = xLen ;
	m_fYAxisLength = yLen ;
	m_fZAxisLength = zLen ;
}

ThreeDAxis::~ThreeDAxis(void)
{
}

HRESULT ThreeDAxis::InitiVertex()
{
	// X axis-red
	CUSTOMVERTEX OX = { 0.0f, 0.0f, 0.0f, 0xffff0000 };
	CUSTOMVERTEX X = { 0.5f, 0.0f, 0.0f, 0xffff0000 }; 

	// Y axis-green
	CUSTOMVERTEX OY = { 0.0f, 0.0f, 0.0f, 0xff00ff00 }; 
	CUSTOMVERTEX Y = { 0.0f, 0.5f, 0.0f, 0xff00ff00 }; 

	// Z axis-blue
	CUSTOMVERTEX OZ = { 0.0f, 0.0f, 0.0f, 0xff0000ff }; 
	CUSTOMVERTEX Z = { 0.0f, 0.0f, 0.5f, 0xff0000ff };  

	// arrow x
	CUSTOMVERTEX X1 = { 0.45f, 0.05f, 0.0f, 0xffff0000 }; 
	CUSTOMVERTEX X2 = { 0.45f, -0.05f, 0.0f, 0xffff0000 }; 
	
	// arrow x
	CUSTOMVERTEX Y1 = { -0.05f, 0.45f, 0.0f, 0xff00ff00 }; 
	CUSTOMVERTEX Y2 = { 0.05f, 0.45f, 0.0f, 0xff00ff00 }; 

	// arrow z
	CUSTOMVERTEX Z1 = { -0.05f, 0.0f, 0.45f, 0xff0000ff }; 
	CUSTOMVERTEX Z2 = { 0.05f, 0.0f, 0.45f, 0xff0000ff }; 

	// letter X
	CUSTOMVERTEX XA = { 0.65f, 0.05f, 0.0f, 0xffff0000 };
	CUSTOMVERTEX XB = { 0.75f, -0.05f, 0.0f, 0xffff0000 }; 
	CUSTOMVERTEX XC = { 0.65f, -0.05f, 0.0f, 0xffff0000 }; 
	CUSTOMVERTEX XD = { 0.75f, 0.05f, 0.0f, 0xffff0000 }; 

	// letter Y
	CUSTOMVERTEX YA = { -0.05f, 0.75f, 0.0f, 0xff00ff00 };
	CUSTOMVERTEX YB = { 0.05f, 0.75f, 0.0f, 0xff00ff00 }; 
	CUSTOMVERTEX YC = { 0.0f, 0.7f, 0.0f, 0xff00ff00 };
	CUSTOMVERTEX YD = { 0.0f, 0.65f, 0.0f, 0xff00ff00 }; 

	// letter Z
	CUSTOMVERTEX ZA = { -0.05f, 0.0f, 0.65f, 0xff0000ff };  
	CUSTOMVERTEX ZB = { -0.05f, 0.0f, 0.7f, 0xff0000ff }; 
	CUSTOMVERTEX ZC = { 0.05f, 0.0f, 0.65f, 0xff0000ff };  
	CUSTOMVERTEX ZD = { 0.05f, 0.0f, 0.7f, 0xff0000ff };  

	
	CUSTOMVERTEX vertices[] =
	{
		// axis
		OX, X, OY, Y, OZ, Z,

		// arrow 
		X, X1, X, X2, Y, Y1, Y, Y2, Z, Z1, Z, Z2,

		// letter X
		XA, XB, XC, XD, 

		// letter Y
		YA, YC, YB, YC, YC, YD,

		// letter Z
		ZA, ZB, ZB, ZC, ZC, ZD,
	} ;

	// Create vertex buffer
	if( FAILED( m_pDirect3DDevice->CreateVertexBuffer( 34 * sizeof(CUSTOMVERTEX),
		0, D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT, &m_pVertexBuffer, NULL ) ) )
	{
		return E_FAIL;
	}

	// copy vertex data
	VOID* pVertices;
	if( FAILED( m_pVertexBuffer->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
		return E_FAIL;
	memcpy( pVertices, vertices, sizeof(vertices) );
	m_pVertexBuffer->Unlock();

	return S_OK;

}

void ThreeDAxis::Draw()
{
	m_pDirect3DDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(CUSTOMVERTEX) );
	m_pDirect3DDevice->SetFVF( D3DFVF_CUSTOMVERTEX );

	//draw line
	m_pDirect3DDevice->DrawPrimitive( D3DPT_LINELIST, 0, 17 );
}

void ThreeDAxis::SetDevice(LPDIRECT3DDEVICE9 device)
{
	m_pDirect3DDevice = device ;
}

